package cn.edu.qzuie.artist.module.td.controller.admin.classinfo;

import cn.edu.qzuie.artist.module.td.controller.admin.major.vo.MajorAndDepartNameRespVo;
import cn.edu.qzuie.artist.module.td.controller.admin.major.vo.MajorPageReqVO;
import org.springframework.web.bind.annotation.*;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;


import java.util.*;
import java.io.IOException;

import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.common.pojo.CommonResult;
import cn.edu.qzuie.artist.framework.common.util.object.BeanUtils;
import static cn.edu.qzuie.artist.framework.common.pojo.CommonResult.success;

import cn.edu.qzuie.artist.framework.excel.core.util.ExcelUtils;

import cn.edu.qzuie.artist.framework.apilog.core.annotation.ApiAccessLog;
import static cn.edu.qzuie.artist.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.edu.qzuie.artist.module.td.controller.admin.classinfo.vo.*;
import cn.edu.qzuie.artist.module.td.dal.dataobject.classinfo.ClassinfoDO;
import cn.edu.qzuie.artist.module.td.service.classinfo.ClassinfoService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - TD班级")
@RestController
@RequestMapping("/td/classinfo")
@Validated
public class ClassinfoController {

    @Resource
    private ClassinfoService classinfoService;

    @PostMapping("/create")
    @Operation(summary = "创建班级")
    @PreAuthorize("@ss.hasPermission('td:classinfo:create')")
    public CommonResult<Long> createClassinfo(@Valid @RequestBody ClassinfoSaveReqVO createReqVO) {
        return success(classinfoService.createClassinfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新班级")
    @PreAuthorize("@ss.hasPermission('td:classinfo:update')")
    public CommonResult<Boolean> updateClassinfo(@Valid @RequestBody ClassinfoSaveReqVO updateReqVO) {
        classinfoService.updateClassinfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除班级")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('td:classinfo:delete')")
    public CommonResult<Boolean> deleteClassinfo(@RequestParam("id") Long id) {
        classinfoService.deleteClassinfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得班级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('td:classinfo:query')")
    public CommonResult<ClassinfoRespVO> getClassinfo(@RequestParam("id") Long id) {
        ClassinfoDO classinfo = classinfoService.getClassinfo(id);
        return success(BeanUtils.toBean(classinfo, ClassinfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得班级分页")
    @PreAuthorize("@ss.hasPermission('td:classinfo:query')")
    public CommonResult<PageResult<ClassinfoRespVO>> getClassinfoPage(@Valid ClassinfoPageReqVO pageReqVO) {
        PageResult<ClassinfoDO> pageResult = classinfoService.getClassinfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ClassinfoRespVO.class));
    }

    @GetMapping("/getMajorAndSchoolNameRespVoPage")
    @Operation(summary = "获得专业和部门名称分页")
    @PreAuthorize("@ss.hasPermission('td:major:query')")
    public CommonResult<PageResult<MajorAndSchoolNameRespVo>> getMajorAndSchoolNameRespVoPage(@Valid ClassinfoPageReqVO pageReqVO) {
        PageResult<MajorAndSchoolNameRespVo> majorAndSchoolNameRespVoPage = classinfoService.getMajorAndSchoolNameRespVoPage(pageReqVO);
        return success(BeanUtils.toBean(majorAndSchoolNameRespVoPage, MajorAndSchoolNameRespVo.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出班级 Excel")
    @PreAuthorize("@ss.hasPermission('td:classinfo:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClassinfoExcel(@Valid ClassinfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClassinfoDO> list = classinfoService.getClassinfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "班级.xls", "数据", ClassinfoRespVO.class,
                        BeanUtils.toBean(list, ClassinfoRespVO.class));
    }
}